package com.company.site.wenjuan;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.util.WebUtils;

import com.company.base.utils.CheckUtil;
import com.company.base.utils.JSSDKUtil;
import com.company.base.web.servlet.TokenThread;
import com.company.site.mode.wechatuser.WechatUser;
import com.company.site.mode.wenjuan.Player;
import com.company.site.mode.wenjuan.Question;
import com.company.site.user.UserService;


@Controller
@RequestMapping("wenjuan")
public class WenjuanController{
	
	@Resource
	UserService userService;
	@Resource
	WenjuanService wenjuanService;
	
	//获取随机下标
	private static Integer randomIndex(Integer l){//范围0~(l-1)
		return Double.valueOf(Math.floor(Double.valueOf(Math.random()*l))).intValue();
	}
	
	//主页
	@RequestMapping(value="main")
	public String main(Model model,HttpServletRequest request,String msg,Integer type) throws UnsupportedEncodingException{
		
		model.addAttribute("countPlayers", wenjuanService.countPlayers());
		System.out.println("countPlayers--------------------:"+wenjuanService.countPlayers());
		//获取微信openid，判断是否关注
		String openid=(String) WebUtils.getSessionAttribute(request, "openid");
		WechatUser wechatUser=userService.getUserInfoByOpenid(openid);
//		if (CheckUtil.isEmpty(wechatUser)||wechatUser.getSubscribe().equals(0)) {
//			System.out.println("未关注的用户！");
//			model.addAttribute("error", "error");
//			return "redirect:sub.htm";
//		}
		Player player = wenjuanService.getPlayerByOpenid(openid);
		if (!CheckUtil.isEmpty(player)) {
			model.addAttribute("player", player);
		}
		model.addAttribute("type", type);
		return "wenjuan/main";
	}
	
	//关注
	@RequestMapping(value="sub")
	public String sub(Model model,HttpServletRequest request){
		return "wenjuan/sub";
	}
	
	//答题
	Long[][] resultIdsAll = {							//题目三种组合
			 { 1l, 6l, 2l, 3l, 4l, 5l, 7l, 8l,10l,12l}
			,{ 9l,11l,13l,15l,16l,17l,18l,19l,20l,21l}
			,{14l,22l,23l,24l,25l,26l,27l,28l,29l,30l}
			};
//	Long[] resultIds = resultIdsAll[0];					//取第一种
	@RequestMapping(value="question")
	public String question(Model model,HttpServletRequest request,String msg,Integer type) throws UnsupportedEncodingException{

		if (CheckUtil.isEmpty(type)||!(type>=0&&type<resultIdsAll.length)) {
			type=0;
		}
		Long[] resultIds = resultIdsAll[type];					//
//		Integer questionSize = 10;
//		Integer t1Size = 5;
//		Integer t2Size = 5;
		
		//测试用
//		WebUtils.setSessionAttribute(request, "openid", "oDKLWs7mryL7Koln-t8FGqu-qNh4");
		
		//获取微信openid，判断是否关注
		String openid=(String) WebUtils.getSessionAttribute(request, "openid");
		WechatUser wechatUser=userService.getUserInfoByOpenid(openid);
//		if (CheckUtil.isEmpty(wechatUser)||wechatUser.getSubscribe().equals(0)) {
//			System.out.println("未关注的用户！");
//			model.addAttribute("error", "error");
//			return "redirect:sub.htm";
//		}
//		//是否答题
//		Player player = wenjuanService.getPlayerByOpenid(openid);
//		//题目已生成，已提交
//		if (!CheckUtil.isEmpty(player)
//				&&Integer.valueOf(2).equals(player.getStatus())) {
//			model.addAttribute("resultIds", player.getQuestions());
//			return "redirect:result.htm";
//		}

		
		//生成题目
//		Long[] resultIds = new Long[questionSize];
		
		List<Question> questions = wenjuanService.getQuestions();
		List<Question> resultQuestion = new ArrayList<Question>();
		for (int j = 0; j < resultIds.length; j++) {
			//根据id从所有题目questions中取出对应id的题目Question，加入到返回结果resultQuestion中
			for (int i = 0; i < questions.size(); i++) {
				if (resultIds[j].equals(questions.get(i).getId())) {
					resultQuestion.add(questions.get(i));
				}
			}
		}
		String resultIdsStr = "";
		for (int i = 0; i < resultIds.length; i++) {
			if (i!=0) {
				resultIdsStr+=",";
			}
			resultIdsStr+=resultIds[i].toString();
		}
		model.addAttribute("questions", resultIdsStr);
		//生成题目
		
		//题目已生成，未提交
//		if (!CheckUtil.isEmpty(player)
//				&&Integer.valueOf(1).equals(player.getStatus())) {
//			String[] questionStrArr = player.getQuestions().split(",");
//			for (int i = 0; i < resultIds.length; i++) {
//				System.out.println("//题目已生成，未提交--题目编号"+i+":"+questionStrArr[i]);
//				resultIds[i] = Long.valueOf(questionStrArr[i]);
//			}
//			model.addAttribute("questions", player.getQuestions());
//			//返回卷面
//		}

		
//		//题目未生成
////		if (CheckUtil.isEmpty(player)) {
//			List<Question> type1Question = new ArrayList<Question>();//判断
//			List<Question> type2Question = new ArrayList<Question>();//选择
//			String[] rightAnswers = new String[questionSize]; 
//			for (int j = 0; j < questions.size(); j++) {
//				//题目类型
//				Integer type = questions.get(j).getType();
//				if (Integer.valueOf(1).equals(type)) {
//					type1Question.add(questions.get(j));//判断
//				}
//				if (Integer.valueOf(2).equals(type)) {
//					type2Question.add(questions.get(j));//选择
//				}
//			}
//			Long[] typ1 = new Long[t1Size];
//			String[] ra1 = new String[t1Size];
//			//判断-随机获取
//			for (int i = 0; i < t1Size; i++) {
//				Integer l = type1Question.size();
//				Integer index = randomIndex(l);//范围0~（l-1）
//				typ1[i]=type1Question.get(index).getId();
//				ra1[i]=type1Question.get(index).getRight();
//				type1Question.remove(index.intValue());//
//			}
//			Long[] typ2 = new Long[t2Size];
//			String[] ra2 = new String[t2Size];
//			//选择-随机获取
//			for (int i = 0; i < t2Size; i++) {
//				Integer l = type2Question.size();
//				Integer index = randomIndex(l);//范围0~（l-1）
//				typ2[i]=type2Question.get(index).getId();
//				ra2[i]=type2Question.get(index).getRight();
//				type2Question.remove(index.intValue());//
//			}
//			//拼接结果数组
//			System.arraycopy(typ1, 0, resultIds, 0          , typ1.length);
//			System.arraycopy(typ2, 0, resultIds, typ1.length, typ2.length);
//			System.arraycopy(ra1 , 0, rightAnswers, 0          , ra1.length);
//			System.arraycopy(ra2 , 0, rightAnswers, ra1.length , ra2.length);
//			String resultIdsStr = "";
//			String resultRasStr = "";
//			for (int i = 0; i < resultIds.length; i++) {
//				if (i!=0) {
//					resultIdsStr+=",";
//				}
//				resultIdsStr+=resultIds[i].toString();
//			}
//			for (int i = 0; i < rightAnswers.length; i++) {
//				if (i!=0) {
//					resultRasStr+=",";
//				}
//				resultRasStr+=rightAnswers[i].toString();
//			}
//			
////			Player newPlayer = new Player();
////			newPlayer.setOpenid(openid);
////			newPlayer.setQuestions(resultIdsStr);
////			newPlayer.setRightAnswers(resultRasStr);
////			wenjuanService.addPlayer(newPlayer);
//			model.addAttribute("questions", resultIdsStr);
////		}
//		for (int i = 0; i < resultIds.length; i++) {
//			resultQuestion.add(questions.get(resultIds[i].intValue()-1));
//		}
		//获取题目（所有题目、随机挑选、返回卷面以及题目编号数组和正确答案数组）（插入数据库）
		
		//向页面返回卷面
		model.addAttribute("result", resultQuestion);
	
		return "wenjuan/question";
	}

	Integer questionSize = 10;
	
	@RequestMapping(value="answer")
	public String answer(Model model,HttpServletRequest request,String subQuestions,String subAnswers) throws UnsupportedEncodingException{
		
		//获取微信openid，判断是否关注
		String openid=(String) WebUtils.getSessionAttribute(request, "openid");
		WechatUser wechatUser=userService.getUserInfoByOpenid(openid);
//		if (CheckUtil.isEmpty(wechatUser)||wechatUser.getSubscribe().equals(0)) {
//			System.out.println("未关注的用户！");
//			model.addAttribute("error", "error");
//			return "redirect:sub.htm";
//		}
		//是否完全答题
		if (CheckUtil.isEmpty(subAnswers)) {
			System.out.println("-------------subAnswersisEmpty-------------");
			return "wenjuan/msg";
		}
		String[] subAnswersArr = subAnswers.split(",");
		if (!questionSize.equals(subAnswersArr.length)) {
			System.out.println("-------------subAnswersArr.length error-------------");
			return "wenjuan/msg";
		}
		//获取player信息
//		Player player = wenjuanService.getPlayerByOpenid(openid);
		//判断答题状态
//		if (!player.getStatus().equals(1)) {
//			return "wenjuan/msg";
//		}
		//对比答案，得到正确数量，并更新数据库
		String[] subQuestionsArr = subQuestions.split(",");
		List<Question> questions = wenjuanService.getQuestions();
		Integer rightCount = 0;
		
		List<String> answerList = new ArrayList<>();
		
		for (int i = 0; i < subQuestionsArr.length; i++) {
			Long id = Long.valueOf(subQuestionsArr[i]);
			String ans = subAnswersArr[i].trim().toUpperCase();//答案去空格转大写
			for (int j = 0; j < questions.size(); j++) {
				if (questions.get(j).getId().equals(id)) {
					if (questions.get(j).getRight().equals(ans)) {
						rightCount+=1;
						System.out.println("正确---id："+id.toString()+",答案"+questions.get(j).getRight());
					}
					else{
						System.out.println("错误---id："+id.toString()+",您的答案"+ans+",正确答案："+questions.get(j).getRight());
					}
				}
			}
		}
		Player player = new Player();
		player.setRightCount(rightCount);
		player.setAnswers(subAnswers);
		player.setOpenid(openid);
		player.setQuestions(subQuestions);
//		player.setRightAnswers(resultRasStr);
		Long newId = wenjuanService.addPlayer(player);
		//向页面返回正确数量
		model.addAttribute("player", player);
		return "redirect:result.htm?id="+player.getId();
	}
	
	@RequestMapping(value="result")
	public String result(Model model,HttpServletRequest request,String id){

		//拼接字符串-当前请求
		StringBuffer requestUrl = request.getRequestURL();
        String queryString = request.getQueryString();
        String url = requestUrl.toString();
        if(!CheckUtil.isEmpty(queryString)){
        	url = requestUrl +"?"+queryString;
        }
        //微信分享事件---jssdk
		/*Map<String, Object> map=new HashMap<>();*/
		Map<String, String> mapJSSDK=JSSDKUtil.sign(TokenThread.jsapi_ticket, url);
		
		model.addAttribute("mapJSSDK", mapJSSDK);
		
		
		String openid=(String) WebUtils.getSessionAttribute(request, "openid");
		Player player = new Player();
		if (!CheckUtil.isEmpty(id)) {
			player = wenjuanService.getPlayerById(Long.valueOf(id));
		}
		else if (!CheckUtil.isEmpty(openid)) {
			player = wenjuanService.getPlayerByOpenid(openid);
		}
		else {
			return "redirect:main.htm";
		}
		
		if (CheckUtil.isEmpty(player)) {
			return "wenjuan/msg";
		}
		
		
		String subAnswers = player.getAnswers();
		String subQuestions = player.getQuestions();
		if (CheckUtil.isEmpty(subAnswers)) {
			System.out.println("-------------subAnswersisEmpty-------------");
			return "wenjuan/msg";
		}
		String[] subAnswersArr = subAnswers.split(",");
		if (!questionSize.equals(subAnswersArr.length)) {
			System.out.println("-------------subAnswersArr.length error-------------");
			return "wenjuan/msg";
		}
		//对比答案
		String[] subQuestionsArr = subQuestions.split(",");
		List<Question> questions = wenjuanService.getQuestions();
		Integer rightCount = 0;
		
		List<Question> resultList = new ArrayList<>();
		
		for (int i = 0; i < subQuestionsArr.length; i++) {
			Long Qid = Long.valueOf(subQuestionsArr[i]);
			String ans = subAnswersArr[i].trim().toUpperCase();//答案去空格转大写
			for (int j = 0; j < questions.size(); j++) {
				if (questions.get(j).getId().equals(Qid)) {
					Question result = questions.get(j);
					result.setYours(ans);
					if (questions.get(j).getRight().equals(ans)) {
						rightCount+=1;
						result.setCorrectStatus(1);
//						result.setRightAnswer(rightAnswer);
//						result.setYourAnswer(yourAnswer);
						/*result.*/
						System.out.println("正确---id："+id.toString()+",答案"+questions.get(j).getRight());
					}
					else{
						result.setCorrectStatus(2);
						System.out.println("错误---id："+id.toString()+",您的答案"+ans+",正确答案："+questions.get(j).getRight());
					}
					resultList.add(result);
				}
			}
		}
		
		
		
		model.addAttribute("player", player);
		model.addAttribute("result", resultList);
		
		return "wenjuan/result";
	}
	
	
	
	
//	//投票首页
//	@RequestMapping(value="wenjuan")
//	public String vote(Player player,Model model,HttpServletRequest request,String msg) throws UnsupportedEncodingException{
//		//测试用
////		WebUtils.setSessionAttribute(request, "openid", "oDKLWs7mryL7Koln-t8FGqu-qNh4");
//		
//		if (!CheckUtil.isEmpty(player.getKey())) {
//			player.setKey(new String(player.getKey().getBytes("ISO-8859-1"),"UTF-8"));
//			model.addAttribute("key", player.getKey());
//		}
//		
//		String openid=(String) WebUtils.getSessionAttribute(request, "openid");
///*		WechatUser wechatUser=userService.getUserInfoByOpenid(openid);
//		if (CheckUtil.isEmpty(wechatUser)||wechatUser.getSubscribe().equals(0)) {
//			System.out.println("未关注的用户！");
//			model.addAttribute("error", "error");
//		}*/
//		voteService.addTotalVisted();
//		Statistics statistics=voteService.findStatistics();
//		List<Player> list = voteService.findPlayerInfo(player);
//		model.addAttribute("list", list);
//		model.addAttribute("totalPlayer", statistics.getTotalPlayer());
//		model.addAttribute("totalVisted", statistics.getTotalVisted());
//		model.addAttribute("totalVoted", statistics.getTotalVoted());
//		//String openid=(String) WebUtils.getSessionAttribute(request, "openid");
//		model.addAttribute("openid", openid);
//		model.addAttribute("id", player.getId());
//		model.addAttribute("msg", msg);
//		
//		return "vote/vote";
//	}
//	
//	//进行投票
//	@RequestMapping(value="doVote",produces="application/text;charset=utf-8")
//	@ResponseBody
//	public String doVote(Long id,HttpServletRequest request) throws ParseException{
//		String openid=(String) WebUtils.getSessionAttribute(request, "openid");
//
//		Statistics statistics=voteService.findStatistics();
//	    String endDateStr = statistics.getEndDate();//"2016-03-31 23:59:59"
//	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//	    Date enDate = sdf.parse(endDateStr);
//	    Date nowDate = new Date();
//	    if (nowDate.after(enDate)) {
//	      return "投票已经截至，感谢您的参与！";
//	    }
//		if(CheckUtil.isEmpty(openid)){
//			return "参数错误！";
//		}
//		//String openid="oQSRbuHYciRcHXmFAEwRJmnKeVzY";
//		WechatUser wechatUser=userService.getUserInfoByOpenid(openid);
//		List<Vote> list=voteService.findTodayVoteListByOpenid(openid);
//		Vote vote=new Vote();
//		vote.setOpenid(openid);
//		vote.setPlayer(id);
//		//check 今日投票次数，
//		if(list.size()==0){
//			//vote表增加投票记录
//			voteService.addVote(vote);
//			//player表增加投票数
//			voteService.addPlayerVotedNum(id);
//			//statistics表增加投票数
//			voteService.addTotalVoted();
//		}else{
//			Date lastVotedTime=formatDate(wechatUser.getLastVotedTime());
//			Date nowTime=formatDate(new Date());
//			if(lastVotedTime.compareTo(nowTime)==0){
//				if (list.size()<3) {
//					for (Vote v : list) {
//						if(id.equals(v.getPlayer())){
//							return "对不起，每人每天您只能投一次票！";
//						}
//					}
//					//vote表增加投票记录
//					voteService.addVote(vote);
//					//player表增加投票数
//					voteService.addPlayerVotedNum(id);
//					//statistics表增加投票数
//					voteService.addTotalVoted();
//				}else{
//					return "对不起，今天的次数已经用完！";
//				}
//			}else if (lastVotedTime.compareTo(nowTime)==-1) {
//				for (Vote v : list) {
//					if(id.equals(v.getPlayer())){
//						return "对不起，每人每天您只能投一次票！";
//					}
//				}
//				//vote表增加投票记录
//				voteService.addVote(vote);
//				//player表增加投票数
//				voteService.addPlayerVotedNum(id);
//				//statistics表增加投票数
//				voteService.addTotalVoted();
//			}else {
//				return "系统时间错误！";
//			}
//		}
////		//check 今日投票次数，
////		if(CheckUtil.isEmpty(wechatUser.getLastVotedTime())){
////			wechatUser.setVotedPlayer(id);
////			wechatUser.setVotedCount(1);
////			//vote表增加投票记录
////			voteService.addVote(vote);
////			//player表增加投票数
////			voteService.addPlayerVotedNum(id);
////			//statistics表增加投票数
////			voteService.addTotalVoted();
////		}else{
////			Date lastVotedTime=formatDate(wechatUser.getLastVotedTime());
////			Date nowTime=formatDate(new Date());
////			if(lastVotedTime.compareTo(nowTime)==0){
////				if (wechatUser.getVotedCount().intValue()<3) {
////					if(!id.equals(wechatUser.getVotedPlayer())){
////						return "对不起，您只能投票给同一个人";
////					}else {
////						wechatUser.setVotedCount(wechatUser.getVotedCount()+1);
////						//vote表增加投票记录
////						voteService.addVote(vote);
////						//player表增加投票数
////						voteService.addPlayerVotedNum(id);
////						//statistics表增加投票数
////						voteService.addTotalVoted();
////					}
////				}else{
////					return "对不起，今天的次数已经用完！";
////				}
////			}else if (lastVotedTime.compareTo(nowTime)==-1) {
////				if(!id.equals(wechatUser.getVotedPlayer())){
////					return "对不起，您只能投票给同一个人";
////				}else {
////					wechatUser.setVotedCount(1);
////					//vote表增加投票记录
////					voteService.addVote(vote);
////					//player表增加投票数
////					voteService.addPlayerVotedNum(id);
////					//statistics表增加投票数
////					voteService.addTotalVoted();
////				}
////			}else {
////				return "系统时间错误！";
////			}
////		}
//		userService.updateVoteWechatuser(wechatUser);
//		return "success";
//	}
//	
//	//下拉/点击加载
////	<div class="item">
////	<div class="item-content">
////		<div class="box">
////			<div class="pic">
////				<a href="<%=basePath%>/voteinfo.htm?id=${player.id }">
////					<img src="<%=basePath%>/upload/${player.img}" alt="" />
////				</a>
////				<span class="g" style="padding: 2px; display: block; position: absolute; top: 0; left: 0; font-size: 12px; color: #fff; background: rgba(0, 0, 0, 0.5)">编号:${player.id }</span>
////				<span class="g" style="padding: 2px 0; display: block; width: 100%; position: absolute; bottom: 0; left: 0; font-size: 12px; color: #fff; background: rgba(0, 0, 0, 0.5)"></span>
////			</div>
////			<div class="info">
////				<span>${player.name }</span>
////			</div>
////			<div class="btnwarp">
////				<a href="javascript:void(0);" class="btnvote" itemId="${player.id }" propertyId="1">投票(<span class="vc">${player.votedNum }</span>)
////				</a>
////			</div>
////		</div>
////	</div>
////</div>
//	@RequestMapping(value="loadVote",produces="application/text;charset=utf-8")
//	@ResponseBody
//	public String loadVote(Integer page,HttpServletRequest request){
//		String path = request.getContextPath();
//		String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()+ path;
//		StringBuilder sb=new StringBuilder();
//		List<Player> list=voteService.loadVote(page*10);
//		for (Player player : list) {
//			sb.append("<div class='item'><div class='item-content'><div class='box'><div class='pic'>");
//			sb.append("<a href='"+basePath+"/voteinfo.htm?id="+player.getId()+"'>");
//			if(CheckUtil.isEmpty(player.getCompress())){
//				sb.append("<img src='"+basePath+"/upload/"+player.getImg()+"' alt='' />");
//			}
//			else{
//				sb.append("<img src='"+basePath+"/upload/"+player.getCompress()+"' alt='' />");
//			}
//			sb.append("</a>");
//			sb.append("<span class='g' style='padding: 2px; display: block; position: absolute; top: 0; left: 0; font-size: 12px; color: #fff; background: rgba(0, 0, 0, 0.5)'>编号:"+player.getId()+"</span>");
//			sb.append("<span class='g' style='padding: 0.5px 0; display: block; width: 100%; position: absolute; bottom: 0; left: 0; font-size: 12px; color: #fff; background: rgba(0, 0, 0, 0.5)'></span>");
//			sb.append("</div><div class='info'>");
//			sb.append("<p style='width:100%;text-align:center'>"+player.getTitle()+"</p>");
//			sb.append("</div><div class='btnwarp'>");
//			sb.append("<a href='javascript:void(0);' class='btnvote' itemId='"+player.getId()+"' propertyId='1'>投票(<span class='vc'>"+player.getVotedNum()+"</span>)");
//			sb.append("</a></div></div></div></div>");
//		}
//		
//		return sb.toString();
//	}
//	
//	//player详情
//	@RequestMapping("voteinfo")
//	public String voteinfo(Long id,Model model,HttpServletRequest request){
//		String openid=(String) WebUtils.getSessionAttribute(request, "openid");
//		WechatUser wechatUser=userService.getUserInfoByOpenid(openid);
//		if (CheckUtil.isEmpty(wechatUser)||wechatUser.getSubscribe().equals(0)) {
//			System.out.println("未关注的用户！");
//			model.addAttribute("error", "error");
//		}
//		Player player=voteService.findPlayerById(id);
////		//距上一名票数差距
////		List<Player> playList=voteService.findRankPlayer();
//		
////		Integer rank = player.getRank();
//
////		Integer diff=0;
////		if (rank>=1) {
////			diff=0;
////		}
////		else {
////			for (int i = 0; i < playList.size(); i++) {
////				if (playList.get(i).getRank()==(rank-1)) {
////					diff=playList.get(i).getVotedNum()-player.getVotedNum();
////				}
////			}
////		}
////		model.addAttribute("diff", diff);
//		//图片list
//		if (!CheckUtil.isEmpty(player.getCompress())) {
//			List<String> compressList = Arrays.asList(player.getCompress().split(","));
//			model.addAttribute("compressList", compressList);
//		}
//		model.addAttribute("player", player);
//		return "vote/info";
//	}
//	
//	//voteUpload
//	@RequestMapping("voteUpload")
//	public String voteUpload(HttpServletRequest request,Model model){
//		String openid=(String) WebUtils.getSessionAttribute(request, "openid");
//		WechatUser wechatUser=userService.getUserInfoByOpenid(openid);
//		if (CheckUtil.isEmpty(wechatUser)
//				||wechatUser.getSubscribe().equals(0)) {
//			model.addAttribute("error", "error");
//		}
//		if (!CheckUtil.isEmpty(openid)) {
//			Player player = new Player();
//			player.setOpenid(openid);
//			if (!CheckUtil.isEmpty(voteService.findPlayerInfo(player))) {
//				return "redirect:vote.htm?msg='havejoin'";
//			}
//		}
//		
//		
//		
//		return "vote/upload";
//	}
//	
//	//joinVote
//	@RequestMapping("joinVote")
//	@ResponseBody
//	public String joinVote(Player player,HttpServletRequest request) throws ParseException{
//		String openid=(String) WebUtils.getSessionAttribute(request, "openid");
//		player.setOpenid(openid);
//
//		Statistics statistics=voteService.findStatistics();
//		String endDateStr = statistics.getEndDate();//"2016-03-31 23:59:59"
//	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//	    Date enDate = sdf.parse(endDateStr);
//	    Date nowDate = new Date();
//	    if (nowDate.after(enDate)) {
//	      return "redirect:vote.htm?msg='end'";
//	    }
//
//		String imgStr = player.getImg();
//		if (!CheckUtil.isEmpty(imgStr)) {
//			String imgArray[] = imgStr.split(",");
//			String compressStr = "";
//			for (int i = 0; i < imgArray.length; i++) {
//				if(!imgArray[i].contains("webp")&&!imgArray[i].contains("gif")){
//					String addimg="";
//					if (i==0) {
//						addimg="";
//					}
//					else {
//						addimg=",";
//					}
//					addimg += ((imgArray[i]).split("\\."))[0]+"$2"+"."+((imgArray[i]).split("\\."))[1];
//					compressStr +=addimg;
//				}
//			}
//			player.setCompress(compressStr);
//		}
//		
//		/*if(!player.getImg().contains("webp")&&!player.getImg().contains("gif")){
//			player.setCompress(((player.getImg()).split("\\."))[0]+"x2"+"."+((player.getImg()).split("\\."))[1]);
//		}*/
//		voteService.addPlayer(player);
//		voteService.addTotalPlayer();
//		return "success";
//	}
//	
//	//uploadImg
//	@RequestMapping("uploadImg")
//	@ResponseBody
//	public String uploadImg(Model model,@RequestParam MultipartFile[] myfiles, HttpServletRequest request) throws Exception{
//		String img="";
//		for (MultipartFile myfile : myfiles) {
//			if (myfile.isEmpty()) {
//				//TODO
//			} else {
//				//获得upload路径
//				String uploadPath = request.getSession().getServletContext().getRealPath("/upload");
//				//文件类型
//				System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"+myfile.getOriginalFilename());
//				String Imagetype=((myfile.getOriginalFilename()).split("\\."))[1];
//				//生成上传后的文件名+后缀
//				String fileName=UUID.randomUUID().toString();
//				String originalImageName=fileName+"."+Imagetype;
//				//保存上传的文件
//				FileUtils.copyInputStreamToFile(myfile.getInputStream(),new File(uploadPath, originalImageName));
//				if(!Imagetype.contains("webp")&&!Imagetype.contains("gif")){
//					//生成缩略图150X170
//					ImageCompressUtil.saveMinPhoto(myfile.getInputStream(), uploadPath+"\\"+fileName+"$2."+Imagetype, 1000, 0.9d);
//					//ReverseImage.checkReverse(uploadPath+"\\"+fileName+"$2."+Imagetype, 90,null);
//					//ImageCompressUtil.zipImageFile(myfile.getInputStream(), 150, 170, 1f, "x2", uploadPath+"\\"+originalImageName);
//				}
//				img=originalImageName;
//			}
//		}
//		return img;
//	}
//	public Date formatDate(Date d) throws ParseException{
//		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
//		d=sdf.parse(sdf.format(d));
//		return d;
//	}
//	
//	@RequestMapping("voteNote")
//	public String voteNote(){
//		return "vote/votenote";
//	}
//	@RequestMapping("votePrice")
//	public String votePrice(){
//		return "vote/voteprice";
//	}
//	
//	@RequestMapping("sub")
//	public String sub(){
//		return "vote/sub";
//	}
//	@RequestMapping("voteRank")
//	public String voteRank(Model model){
//		List<Player> list=voteService.findRankPlayer();
//		model.addAttribute("list", list);
//		return "vote/voterank";
//	}
//	@RequestMapping("cc")
//	public void cc(){
//		List<Player> list=voteService.findRankPlayer();
//		for (Player player : list) {
//			if(!player.getImg().contains("webp")){
//				//ImageCompressUtil.zipImageFile("d:/upload/"+player.getImg(), 150, 170, 1f, "x2");
//				player.setCompress(((player.getImg()).split("\\."))[0]+"x2"+"."+((player.getImg()).split("\\."))[1]);
//				voteService.updatePlayerC(player);
//			}
//		}
//	}
//	
//	@RequestMapping("main")
//	public String main(){
//		return "vote/login";
//	}
//	
//	@RequestMapping("login")
//	public String login(String username,String password,Model model,HttpServletRequest request){
//		if(CheckUtil.isEmpty(username)||CheckUtil.isEmpty(password)){
//			model.addAttribute("msg", "用户名或者密码不能为空！");
//			return "vote/login";
//		}
//		if(!"ldfman@qq.com".equals(username)||!"LDF12345".equals(password)){
//			model.addAttribute("msg", "用户名或者密码错误");
//			return "vote/login";
//		}
//		request.getSession().setAttribute("login", "login");
//		return "redirect:fixNum.htm";
//	}
//	
//	@RequestMapping("fixNum")
//	public String fixNum(Model model,HttpServletRequest request){
//		String login=(String) WebUtils.getSessionAttribute(request, "login");
//		if(!"login".equals(login)){
//			return "redirect:main.htm";
//		}
//		return "vote/cheat";
//	}
//	
//	@RequestMapping("cheat")
//	@ResponseBody
//	public String cheat(Integer num,Long id,HttpServletRequest request){
//		String login=(String) WebUtils.getSessionAttribute(request, "login");
//		if(!"login".equals(login)){
//			return "none";
//		}
//		if(CheckUtil.isEmpty(num)||CheckUtil.isEmpty(id)){
//			return "empty";
//		}
//		Map<String, Object> map=new HashMap<String, Object>();
//		map.put("num", num);
//		map.put("id", id);
//		voteService.cheatTotalVoted(map);
//		voteService.cheatPlayerVotedNum(map);
//		List<Vote> list=new ArrayList<Vote>();
//		for(int i=0;i<num;i++){
//			Vote vote=new Vote();
//			vote.setOpenid("Cheated!");
//			vote.setPlayer(id);
//			list.add(vote);
//		}
//		voteService.cheatVote(list);
//		return "success";
//	}
}
